package com.surpassun.emall.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Index;

@Entity
public class Role {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	
	@Index(name = "INDEX_ROLE_NAME")
	@NotNull
	@Column(unique = true)
	private String name;
	
	private String description;
	
	@ManyToMany
	@JoinTable(name = "Roles_Permissions", joinColumns = @JoinColumn(name="roleId"), inverseJoinColumns = @JoinColumn(name="permissionId"))
	private List<Permission> permissions;
	
	@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
	private List<User> users;

	public Role() {
	}
	
	public Role(String name, String description) {
		super();
		this.name = name;
		this.description = description;
	}
	
	public Role(String name, String description, List<Permission> permissions) {
		super();
		this.name = name;
		this.description = description;
		this.permissions = permissions;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}
